package yzy.ecps.service.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yzy.ecps.dao.ConsoleLogDao;
import yzy.ecps.dao.GoodsClobDao;
import yzy.ecps.dao.GoodsDao;
import yzy.ecps.dao.ParaValueDao;
import yzy.ecps.dao.SkuDao;
import yzy.ecps.model.BtcConsoleLog;
import yzy.ecps.model.BtcGoods;
import yzy.ecps.model.BtcGoodsClob;
import yzy.ecps.model.BtcParaValue;
import yzy.ecps.model.BtcSku;
import yzy.ecps.query.condition.GoodsQueryCondition;
import yzy.ecps.query.condition.PortalGoodsCondition;
import yzy.ecps.service.GoodsService;
import yzy.ecps.utils.EcpsUtils;
import yzy.ecps.utils.PageInfo;
import yzy.ecps.ws.stub.WSGoods;
import yzy.ecps.ws.stub.WSGoodsService;

@Service
public class GoodsServiceImpl implements GoodsService{
	@Autowired
	private GoodsDao goodsDao;
	@Autowired
	private GoodsClobDao clobDao;
	@Autowired
	private SkuDao skuDao;
	@Autowired
	private ParaValueDao paraValueDao;
	@Autowired
	private ConsoleLogDao logDao;

	public PageInfo getPageInfo(GoodsQueryCondition gqc) {
		
		//初始化分页数据
		PageInfo page = new PageInfo(getTotalCount(gqc));
		
		//如果gqc中有页码数据则传入page，没有页码数据则给默认值
		if(gqc.getPageIndex() != null)
			page.setPageIndex(gqc.getPageIndex());
		else
			gqc.setPageIndex(page.getPageIndex());
		
		//如果gqc中没有传入页显示数量，则使用默认值，否则page接收
		if(gqc.getPageSize() == null)
			gqc.setPageSize(page.getPageSize());
		else
			page.setPageSize(gqc.getPageSize());
		
		gqc.setStartNum(page.getStartNum());
		gqc.setEndNum(page.getEndNum());
		//分页设置好后从数据库中查询数据，page接收
		page.setList(goodsDao.findByCondition(gqc));
		
		return page;
	}

	public int getTotalCount(GoodsQueryCondition gqc) {
		return goodsDao.getCountByCondition(gqc);
	}

	public void saveGoods(BtcGoods goods, BtcGoodsClob goodsClob,
			List<BtcSku> skus, List<BtcParaValue> paraValues) {
		
		//设置商品的添加时的默认值，其它默认值在insert语句中已设置
		goods.setCatgoryId(1001L);
		goods.setGoodsNum(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
		//TODO:设置创建人（用户表未创建）
		goods.setCreateUserid(1L);
		
		// 保存商品基本数据后才可获取商品ID
		goodsDao.saveGoods(goods);
		Long goodsId = goods.getGoodsId();
		
		//保存其它关联数据
		clobDao.saveGoodsClobByGoodsId(goodsClob, goodsId);
		skuDao.saveSkusByGoodsId(skus, goodsId);
		paraValueDao.saveParaValuesByGoodsId(paraValues, goodsId);
		
		//后台记录操作日志
		BtcConsoleLog log = new BtcConsoleLog();
		log.setEntityId(goodsId);
		log.setEntityName("商品");
		log.setTableName("BTC_GOODS");
		log.setOpTime(new Date());
		//TODO:设置审核人（用户表未创建）
		log.setOpUserid(1L);
		log.setOpType("商品创建");
		
		logDao.saveLog(log);
		
	}

	@Override
	public void auditGoods(Long goodsId, Integer auditStatus, String notes) {
		//修改商品的状态
		BtcGoods goods = new BtcGoods();
		goods.setGoodsId(goodsId);
		goods.setAuditStatus(auditStatus);
		goods.setAuditTime(new Date());
		//TODO:设置审核人（用户表未创建）
		goods.setAuditUserid(1L);
		
		goodsDao.updateGoods(goods);
		
		BtcConsoleLog log = new BtcConsoleLog();
		log.setEntityId(goodsId);
		log.setEntityName("商品");
		log.setTableName("BTC_GOODS");
		log.setOpTime(new Date());
		//TODO:设置审核人（用户表未创建）
		log.setOpUserid(1L);  
		if(auditStatus == 1	)
			log.setOpType("审核通过");
		else log.setOpType("审核不通过");
		log.setNotes(notes);
		
		logDao.saveLog(log);
		
	}
	
	
	public void showGoods(Long goodsId, Integer showStatus) {
		//修改商品的状态
		BtcGoods goods = new BtcGoods();
		goods.setGoodsId(goodsId);
		goods.setShowStatus(showStatus);
		goods.setOnSaleTime(new Date());
		//TODO:设置修改人
		goods.setUpdateUserid(1L);
		
		goodsDao.updateGoods(goods);
		
		BtcConsoleLog log = new BtcConsoleLog();
		log.setEntityId(goodsId);
		log.setEntityName("商品");
		log.setTableName("BTC_GOODS");
		log.setOpTime(new Date());
		if(showStatus == 1) log.setOpType("上架");
		else log.setOpType("下架");//下架后要删除商品静态页面及对应的地址
		
		//TODO:设置操作人
		log.setOpUserid(1L);
		
		logDao.saveLog(log);
	}

	public PageInfo getPageForPortal(PortalGoodsCondition pgc) {
		
		PageInfo page = new PageInfo(goodsDao.getCountByPortalCondition(pgc));
		if(pgc.getPageIndex()== null)
			pgc.setPageIndex(page.getPageIndex());
		else page.setPageIndex(pgc.getPageIndex());
		if(pgc.getPageSize()==null)
			pgc.setPageSize(page.getPageSize());
		else page.setPageSize(20);
		
		pgc.setStartNum(page.getStartNum());
		pgc.setEndNum(page.getEndNum());
		
		page.setList(goodsDao.findByPortalCondition(pgc));
		
		return page;
	}

	public BtcGoods getGoodsInfo(Long goodsId) {
		return goodsDao.getGoodsById(goodsId);
	}

	public String publishGoods(Long goodsId, String pwd) throws IOException {
		WSGoodsService ws = new WSGoodsService();
		WSGoods wsGoodsPort = ws.getWSGoodsPort();
		String result = wsGoodsPort.publishGoods(goodsId, pwd);
		if(result.equals("success")){
			BtcGoods goods = new BtcGoods();
			goods.setGoodsId(goodsId);
			//设置商品销售时间,发布人ID
			//设置商品的静态页面地址
			String serverPath = EcpsUtils.getValueByKeyFormSysProperties("portalserver_path");
			goods.setStaticPath(serverPath+"/goodsStatic/"+goodsId+".html");
			goods.setOnSaleTime(new Date());
			goods.setUpdateTime(new Date());
			goods.setUpdateUserid(1L);
			goodsDao.updateGoods(goods);
			
			//记录日志
			BtcConsoleLog log = new BtcConsoleLog();
			log.setEntityId(goodsId);
			log.setEntityName("商品");
			log.setTableName("BTC_GOODS");
			log.setOpTime(new Date());
			log.setOpType("发布");
			log.setOpUserid(1L);
			
			logDao.saveLog(log);
		}
		return result;
	}

}
